package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Business;
import cn.wolfcode.domain.Consumption;
import cn.wolfcode.domain.ConsumptionItem;
import cn.wolfcode.domain.SystemDictionary;
import cn.wolfcode.qo.ConsumptionQueryObject;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.*;
import cn.wolfcode.utils.RequestPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/consumption")
public class ConsumptionController {
    @Autowired
    private IConsumptionService consumptionService ;

    @Autowired
    private IConsumptionItemService consumptionItemService;

    @Autowired
    private ISystemDictionaryService systemDictionaryService;

    @Autowired
    private IBusinessService businessService;


    @RequestMapping("/list")
    @RequestPermission(name="明细单查询",expression = "consumption:list")
    public String list(Model model , @ModelAttribute("qo") ConsumptionQueryObject qo){
        PageInfo<Consumption> pageInfo = consumptionService.query(qo);
        model.addAttribute("pageInfo",pageInfo);

        List<SystemDictionary> settlement = systemDictionaryService.selectByBs("settlement_type");
        model.addAttribute("settlement",settlement);

        List<Business> businesses = businessService.selectAll();
        model.addAttribute("businesses",businesses);

        return "consumption/list" ;
    }



    @RequestMapping("/input")
    public String input(Long id,Model model){

        //选择门店
        List<Business> businesses = businessService.selectAll();
        model.addAttribute("businesses",businesses);


        if (id != null) {
            Consumption consumption = consumptionService.selectById(id);
            model.addAttribute("consumption",consumption);

            //查询结算单明细
            List<ConsumptionItem> consumptionItems = consumptionItemService.selectByCno(consumption.getCno());
            model.addAttribute("consumptionItems",consumptionItems);
            //结算类型 settlement_type
            List<SystemDictionary> settlementType = systemDictionaryService.selectByBs("settlement_type");
            model.addAttribute("settlementType",settlementType);
            //选择业务
            List<SystemDictionary> business = systemDictionaryService.selectByBs("business");
            model.addAttribute("business",business);

        }
        return "/consumption/input" ;
    }

    @RequestMapping("/save")
    @RequestPermission(name="明细单编辑(无预约)",expression = "consumption:save")
    public String save(Consumption consumption){
        System.out.println("consumption = " + consumption);
        if (consumption.getId()==null){
            consumptionService.save(consumption);
        }else {
            consumptionService.update(consumption);
        }
        return "redirect:/consumption/list";
    }

    @RequestMapping("/saveOrUpdate")
    @RequestPermission(name="明细单编辑(有预约)",expression = "consumption:saveOrUpdate")
    public String saveOrUpdate(Long id){

        Long newId = consumptionService.saveOrUpdate(id);
        return "redirect:/consumption/input?id="+newId;
    }
}
